﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WarningForeverClone.src.weapons;

namespace WarningForeverClone.src.ships
{
    public class PlayerModel
    {
        int totalTime;
        int levelTime;
        int shipMovingTime;

        int greenLaserTime;
        int blueLaserTime;
        int redLaserTime;

        int greenPlayerHit;
        int bluePlayerHit;
        int redPlayerHit;
        int rocketPlayerHit;

        float shipMovePercentTime;
        float greenLaserPercentTime;
        float blueLaserPercentTime;
        float redLaserPercentTime;

        float greenTurretScore;
        float blueTurretScore;
        float redTurretScore;
        float rocketTurretScore;

        Random random;

        public PlayerModel()
        {
            totalTime = 0;
            levelTime = 0;
            shipMovingTime = 0;

            greenLaserTime = 0;
            blueLaserTime = 0;
            redLaserTime = 0;

            greenPlayerHit = 0;
            bluePlayerHit = 0;
            redPlayerHit = 0;
            rocketPlayerHit = 0;

            random = new Random();
        }

        public void Update(Ship ship)
        {
            totalTime++;
            levelTime++;
            string selectedWeapon = ship.SelectedWeapon;

            if (selectedWeapon == "greenLaser")
            {
                greenLaserTime++;
            }
            else if (selectedWeapon == "blueLaser")
            {
                blueLaserTime++;
            }
            else if (selectedWeapon == "redLaser")
            {
                redLaserTime++;
            }
        }


        public void moveTick()
        {
            shipMovingTime++;
        }

        public void logPlayerHit(Weapon w)
        {
            if (w is GreenLaser)
                greenPlayerHit++;
            else if (w is BlueLaser)
                bluePlayerHit++;
            else if (w is RedLaser)
                redPlayerHit++;
            else if (w is Rocket)
                rocketPlayerHit++;
        }

        public void calculateModel(Ship ship, Boss boss, int level)
        {
            shipMovePercentTime = 1.0f*shipMovingTime / totalTime;
            greenLaserPercentTime = 1.0f * greenLaserTime / totalTime;
            blueLaserPercentTime = 1.0f * blueLaserTime / totalTime;
            redLaserPercentTime = 1.0f * redLaserTime / totalTime;

            greenTurretScore = greenLaserPercentTime + greenPlayerHit * 0.25f;
            blueTurretScore = blueLaserPercentTime + bluePlayerHit * 0.25f;
            redTurretScore = redLaserPercentTime + redPlayerHit * 0.25f + (1 - shipMovePercentTime)/2.5f;
            rocketTurretScore = 0.12f + rocketPlayerHit * 0.25f + (1 - shipMovePercentTime) / 2.5f + (level-1)*0.075f;
        }

        public string GetNewWeaponConfig()
        {
            string wc = "";
            float totalScore = greenTurretScore + blueTurretScore + redTurretScore + rocketTurretScore;

            float greenRange = greenTurretScore / totalScore;
            float blueRange = blueTurretScore / totalScore + greenRange;
            float redRange = redTurretScore / totalScore + blueRange;
            //float rocketRange = rocketTurretScore / totalScore + redRange;

            float greenShieldRange = greenLaserPercentTime;
            float blueShieldRange = blueLaserPercentTime + greenShieldRange;
            float redShieldRange = redLaserPercentTime + blueShieldRange;

            string t = "";
            for (int i = 0; i < 10; i++)
            {
                float r = (float)random.NextDouble();
                if (r >= 0 && r < greenRange)
                    t = "g";
                else if (r >= greenRange && r < blueRange)
                    t = "b";
                else if (r >= blueRange && r < redRange)
                    t = "r";
                else
                    t = "k";

                r = (float)random.NextDouble();
                if (r >= 0 && r < greenShieldRange)
                    t += "0";
                else if (r >= greenShieldRange && r < blueShieldRange)
                    t += "1";
                else
                    t += "2";

                wc += t;
            }


            return wc;
        }

        internal void testPrint()
        {
            Console.WriteLine("shipMove " + shipMovePercentTime);
            Console.WriteLine("greenLaser " + greenLaserPercentTime);
            Console.WriteLine("blueLaser " + blueLaserPercentTime);
            Console.WriteLine("redLaser " + redLaserPercentTime);
        }

        public int LevelTime
        {
            get { return levelTime; }
            set { levelTime = value; }
        }

        public float ShipMovePercentTime
        { 
            get { return shipMovePercentTime; } 
        }

        public float GreenLaserPercentTime 
        {
            get { return greenLaserPercentTime; } 
        }

        public float BlueLaserPercentTime {
            get { return blueLaserPercentTime; } 
        }

        public float RedLaserPercentTime {
            get { return redLaserPercentTime; } 
        }
    }
}
